package com.example.myfoodrecipe.fragments.favorite

import android.content.ClipData
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.myfoodrecipe.data.model.Result
import com.example.myfoodrecipe.databinding.FavoriteItemBinding

class FavAdapter : RecyclerView.Adapter<FavAdapter.ItemViewHolder>() {
    private var recipeList: List<Result> = emptyList()
    class ItemViewHolder(val binding: FavoriteItemBinding): RecyclerView.ViewHolder(binding.root) {
        fun bind(result: Result){
            binding.recipe = result
            binding.executePendingBindings()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        return ItemViewHolder(FavoriteItemBinding.inflate(layoutInflater,parent,false))
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.bind(recipeList[position])
    }

    override fun getItemCount(): Int {
        return recipeList.size
    }

    fun setData(recipes: List<Result>){
        recipeList = recipes
        notifyDataSetChanged()
    }
}